package com.app.service.impl;

import com.app.mapper.IncidentMapper;
import com.app.model.IncidentModel;
import com.app.service.IncidentService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Primary
public class IncidentServiceImpl implements IncidentService {
    @Autowired
    IncidentMapper incidentMapper;

    @Override
    public IncidentModel getIciByIncId(int id) {
        QueryWrapper<IncidentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        IncidentModel role = incidentMapper.selectOne(queryWrapper);
        if (role == null) {
            return null;
        } else {
            return role;
        }
    }

    @Override
    public Page<IncidentModel> getPageIciList(int page, int limit, String eventName, Integer eventType, List<Integer> idList, Integer sortType) {
        QueryWrapper<IncidentModel> queryWrapper = new QueryWrapper<>();
        if (idList == null || idList.size() == 0) {
            queryWrapper.select();
        } else {
            queryWrapper.select().in("department_id", idList);
        }
        if (sortType==null || sortType==1){
            queryWrapper.orderByAsc("number").orderByDesc("create_time");
        }else {
            queryWrapper.orderByDesc("number").orderByDesc("create_time");
        }
        Page<IncidentModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        return incidentMapper.selectPage(pages, queryWrapper);
    }


    @Override
    public void update(IncidentModel model) {
        incidentMapper.updateById(model);
    }

    @Override
    public void insert(IncidentModel model) {
        incidentMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        incidentMapper.deleteById(id);
    }

    @Override
    public Integer incidentCount() {
        QueryWrapper<IncidentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
//        if (idsList != null&&idsList.size()>0) {
//            queryWrapper.in("department_id", idsList);
//        }
        Integer incCount = incidentMapper.selectCount(queryWrapper);
        return incCount;
    }

    @Override
    public List<IncidentModel> selectAll(Integer departmentId) {
        QueryWrapper<IncidentModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", departmentId);
        return incidentMapper.selectList(queryWrapper);
    }

    @Override
    public Result<Object> deleteIncidentBatchByIds(String ids) {
        // 参数校验
        if (StringUtils.isEmpty(ids)) {
            return Result.success("参数为空，无需删除");
        }
        List<Integer> idsList = Arrays.stream(ids.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        // 批量删除
        if (!ListUtils.isEmpty(idsList)) {
            incidentMapper.deleteBatchIds(idsList);
            return Result.success("批量删除成功");
        } else {
            return Result.success("批量删除失败，数据为空");
        }
    }
}
